using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Pegasus.Diagnostics;

namespace Pegasus.Web.UI
{
    /// <summary>
    /// This control extends the UserControl to allow elements of the page to be hidden/shown with a click.
    /// A javascript is automatically included on the page that allows the hide/show behavior.
    /// A method is provided to give a control the power to hide/show another control by calling the javascript.
    /// </summary>
    /// <remarks>
    /// Be sure to call the base OnLoadFinish if you override it in the class that extends this.
    /// This control works with IE 6 no other browsers tested.
    /// </remarks>
    public class ContentHiderControl : UserControlEx
    {
        private string m_methodName = "toggle";

        /// <summary>
        /// Gets or sets the name of the javascript method that will be generated.
        /// The default name is toggle.
        /// </summary>
        public string JavascriptMethodName
        {
            get
            {
                return m_methodName;
            }
            set
            {
                m_methodName = value;
            }
        }

		/// <summary>
		/// Called at the end of the load process.
		/// </summary>
		/// <param name="args">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        protected override void OnLoadFinish( EventArgs args )
        {
            base.OnLoadFinish( args );

            // Create function that toggles visibility of control.
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat( "function {0}(ctl) {{\n", m_methodName );
            sb.Append( "  ctl.style.display = ctl.style.display == '' ? 'none' : ''; }\n" );

            // Register javascript block onto page.
            Page.ClientScript.RegisterClientScriptBlock( Page.GetType(), m_methodName, sb.ToString(), true );
        }

        /// <summary>
        /// Registers a control that can hide/show another control.
        /// </summary>
        /// <param name="hiderControl">
        /// The control that hides/shows another control when clicked.
        /// This must be a WebControl or HtmlControl
        /// </param>
        /// <param name="controlToHide">The control that is hidden/shown.</param>
        public void RegisterHiderControl( Control hiderControl, Control controlToHide )
        {
            RegisterHiddenControl( hiderControl, controlToHide.ClientID, true );
        }

        /// <summary>
        /// Registers a control that can hide/show another control.
        /// </summary>
        /// <param name="hiderControl">
        /// The control that hides/shows another control when clicked.
        /// This must be a WebControl or HtmlControl
        /// </param>
        /// <param name="controlToHide">The control that is hidden/shown.</param>
        /// <param name="setCursorHand">Whether the set the cursor to hand for the hiderControl.</param>
        public void RegisterHiderControl( Control hiderControl, Control controlToHide, bool setCursorHand )
        {
            RegisterHiddenControl( hiderControl, controlToHide.ClientID, setCursorHand );
        }

        /// <summary>
        /// Registers a control that can hide/show another control.
        /// </summary>
        /// <param name="hiderControl">The control that hides/shows another control when clicked.</param>
        /// <param name="controlToHideClientId">The client Id of the control that is hidden/shown.</param>
        public void RegisterHiddenControl( Control hiderControl, string controlToHideClientId )
        {
            RegisterHiddenControl( hiderControl, controlToHideClientId, true );
        }

        /// <summary>
        /// Registers a control that can hide/show another control.
        /// </summary>
        /// <param name="hiderControl">The control that hides/shows another control when clicked.</param>
        /// <param name="controlToHideClientId">The client Id of the control that is hidden/shown.</param>
        /// <param name="setCursorHand">Whether the set the cursor to hand for the hiderControl.</param>
        public void RegisterHiddenControl( Control hiderControl, string controlToHideClientId, bool setCursorHand )
        {
            if( hiderControl is WebControl )
            {
                WebControl webControl = (WebControl) hiderControl;

                // Add onclick event to hiderControl
                webControl.Attributes.Add( "onclick",
                    string.Format( "{0}(document.getElementById('{1}'))", m_methodName, controlToHideClientId ) );

                if( setCursorHand )
                {
                    // Change current cursor style of control to be a hand
                    webControl.Style[ HtmlTextWriterStyle.Cursor ] = "hand";
                }
            }
            else if( hiderControl is HtmlControl )
            {
                HtmlControl htmlControl = (HtmlControl) hiderControl;

                // Add onclick event to hiderControl
                htmlControl.Attributes.Add( "onclick",
                    string.Format( "{0}(document.getElementById('{1}'))", m_methodName, controlToHideClientId ) );

                if( setCursorHand )
                {
                    // Change current cursor style of control to be a hand
                    htmlControl.Style[ HtmlTextWriterStyle.Cursor ] = "hand";
                }

            }
            else
            {
                throw new ArgumentException( "hiderControl must be of type HtmlControl or WebControl" );
            }
        }
    }
}
